//
//  SaveCardViewController.m
//  MyWallet
//
//  Created by 小肥羊 on 15/7/17.
//  Copyright (c) 2015年 小肥羊. All rights reserved.
//

#import "SaveCardViewController.h"

@interface SaveCardViewController ()

@end

@implementation SaveCardViewController

#pragma mark - Set
- (void)setSaveDate:(NSDate *)saveDate
{
    _saveDate = saveDate;
    NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:@"YYYY-MM-dd HH:mm"];
    NSString *locationString=[dateformatter stringFromDate:_saveDate];
    [self.changeDateButton setTitle:locationString forState:UIControlStateNormal];
}

#pragma mark - 初始化
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.selectDateView = [[SelectDateView alloc] initWithDelegate:self];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    NSString *cardIndex = [self.receiveDict objectForKey:@"cardIndex"];
    CardModel *card = [[TotalAssets sharedManager] cardWithIndexString:cardIndex];
    [self.card upCard:card];
    [self.card.activationButton setHidden:YES];
    self.saveDate = [NSDate date];
    [self.changeDateButton.layer setCornerRadius:kNomalRadius];
    self.moneyForSelectView.contents = [[NSMutableArray alloc] initWithArray:Macro_MoneyForSave];
    self.moneyFor = Macro_MoneyForSave[0];
    self.moneyForSelectView.delegate = self;
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self.moneyText becomeFirstResponder];
}


#pragma mark - 切换用途
- (IBAction)changeSavefor:(UISwitch *)sender {
    if (sender.isOn) {
        [self.saveforLabel setText:@"收入"];
        [self.saveLabel setHidden:NO];
        [self.remarkLabel setHidden:NO];
        [self.moneyForSelectView setHidden:NO];
    } else {
        [self.saveforLabel setText:@"存款"];
        [self.saveLabel setHidden:YES];
        [self.remarkLabel setHidden:YES];
        [self.moneyForSelectView setHidden:YES];
    }
    [self setTitle:self.saveforLabel.text];
}

#pragma mark - 完成
- (IBAction)finishDrawCard:(UIBarButtonItem *)sender {
    NSString *cardIndex = [self.receiveDict objectForKey:@"cardIndex"];
    float money = [self.moneyText.text floatValue];
    if (money == 0.0f) {
        UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"。。。" message:@"你什么都没做" delegate:nil cancelButtonTitle:@"是吗" otherButtonTitles:nil];
        [alter show];
    } else {
        if ([self.saveforLabel.text isEqualToString:@"收入"]) {
            [[TotalAssets sharedManager] saveCard:cardIndex Money:money RecordDate:self.saveDate MoneyFor:self.moneyFor Remarks:self.remarkLabel.text];
            [self.navigationController popViewControllerAnimated:YES];
        } else {
            if ([[TotalAssets sharedManager] purseToCard:cardIndex Money:money RecordDate:self.saveDate]) {
                [self.navigationController popViewControllerAnimated:YES];
            } else {
                UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"穷逼" message:@"没那么多钱存啊" delegate:nil cancelButtonTitle:@"好吧" otherButtonTitles:nil];
                [alter show];
            }
        }
    }
}

#pragma mark - 编辑结束
- (IBAction)finishEditTextFiled:(UITextField *)sender {
    
}

#pragma mark - 点击背景
- (IBAction)touchBackView:(UITapGestureRecognizer *)sender {
    [self.view endEditing:YES];
}

#pragma mark - 修改时间
- (IBAction)changeDate:(UIButton *)sender {
    [self.view endEditing:YES];
    [ActionView showView:self.selectDateView direction:PopViewDirectionStyleCenter];
}

#pragma mark - 时间选择代理
- (void)selectDate:(SelectDateView *)picker
{
    self.saveDate = picker.datePicker.date;
    [[NSNotificationCenter defaultCenter] postNotificationName:CLOESNOTICE object:picker];
}
- (void)cancelSelect:(SelectDateView *)picker
{
    [[NSNotificationCenter defaultCenter] postNotificationName:CLOESNOTICE object:picker];
}

#pragma mark - XFYSelectScrollView代理
- (void)operation:(XFYSelectScrollView *)selectView Button:(UIButton *)button AtIndex:(NSInteger)index
{
    [self.view endEditing:YES];
    self.moneyFor = button.titleLabel.text;
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
